package com.example.web.websystem.controller;

import com.example.web.websystem.dao.model.SysAdministratorSettingDO;
import com.example.web.websystem.dao.model.SysGroupDO;
import com.example.web.websystem.response.CommonReturnData;
import com.example.web.websystem.service.SysAdministratorSettingService;
import com.example.web.websystem.service.SysGroupService;
import com.example.web.websystem.token.manage.TokenHandle;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by YangGang on 2021/1/28 10:43。
 */
@Api(tags = "菜单管理（路由path管理）")
@ApiSupport(author = "YangGang")
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController {
    @Autowired
    private SysAdministratorSettingService sysAdministratorSettingService;
    @Autowired
    private SysGroupService sysGroupService;


    /**
     * 查询-系统管理员开放的菜单列表
     *
     * @return
     */
    @GetMapping("/listAdministratorOpenMenus")
    public CommonReturnData listAdministratorOpenMenus() {
        SysAdministratorSettingDO setting = sysAdministratorSettingService.getAdministratorSetting();
        String[] split = setting.getOpenPaths().split(",");
        return CommonReturnData.success(split);
    }

    /**
     * 修改-系统管理员开放菜单
     *
     * @return
     */
    @PostMapping("/updateAdministratorOpenMenus")
    public CommonReturnData updateAdministratorOpenMenus(String openPaths) {
        int i = sysAdministratorSettingService.updateOpenPaths(openPaths);
        return CommonReturnData.success(i);
    }


    /**
     * 查询-当前组织的菜单列表
     *
     * @return
     */
    @GetMapping("/listCurrentGroupMenus")
    public CommonReturnData listCurrentGroupMenus() {
        Long groupId = TokenHandle.getCurrentTokenObject().getGroupId();
        SysGroupDO sysGroup = sysGroupService.getSysGroup(groupId);
        String[] split = sysGroup.getGroupPaths().split(",");
        return CommonReturnData.success(split);
    }

    /**
     * 修改-当前组织的菜单列表
     *
     * @return
     */
    @PostMapping("/updateCurrentGroupMenus")
    public CommonReturnData updateCurrentGroupMenus(String groupPaths) {
        Long groupId = TokenHandle.getCurrentTokenObject().getGroupId();
        int i = sysGroupService.updateSysGroupGroupPaths(groupId, groupPaths);
        return CommonReturnData.success(i);
    }


    /**
     * 修改-指定组织的菜单列表
     *
     * @return
     */
    @PostMapping("/updateGroupMenus")
    public CommonReturnData updateGroupMenus(long groupId, String groupPaths) {
        int i = sysGroupService.updateSysGroupGroupPaths(groupId, groupPaths);
        return CommonReturnData.success(i);
    }


}
